package com.bluefish.bltuan.action.admin;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.AbstractAction;
import com.bluefish.bltuan.annotation.RequireLogin;
import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.shop.Order;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.OrderService;
import com.bluefish.bltuan.service.ShopService;
import com.bluefish.bltuan.tools.shop.AccordType;
import com.bluefish.bltuan.util.PageIndex;

@RequireLogin
@Results({
	@Result(name = "orders", type = "dispatcher", location = "/page/admin/order-list.jsp"),
	@Result(name = "pastTuans", type = "dispatcher", location = "/page/admin/order-past.jsp")
})
public class OrderAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TuanGou tuanGou;
	
	private List<TuanGou> tuans;
	
	private Order order;
	
	private List<Order> orders;
	
	private ShopService shopService;

	private OrderService orderService; 

	private CategoryService categoryService;

	private List<Category> cities;
	
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public List<TuanGou> getTuans() {
		return tuans;
	}

	public void setTuans(List<TuanGou> tuans) {
		this.tuans = tuans;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public TuanGou getTuanGou() {
		return tuanGou;
	}

	public void setTuanGou(TuanGou tuanGou) {
		this.tuanGou = tuanGou;
	}
	
	public List<Category> getCities() {
		return cities;
	}

	public void setCities(List<Category> cities) {
		this.cities = cities;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	/**
	 * 查看不同城市的团购信息
	 */
	@Override
	public HttpHeaders index() {
		PageIndex pageIndex = getPageIndex();
		this.cities = this.categoryService.listCities(pageIndex);
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	@Override
	public HttpHeaders show() {
		//TuanGou tuanGou = this.shopService.findById(getIntegerId());
		this.order = this.orderService.findById(getIntegerId());
		return new DefaultHttpHeaders(SHOW).disableCaching();
	}
	
	/**
	 * 该城市今日团购的订单
	 * @return
	 */
	public String orders(){
		Category city = this.categoryService.getCityByNickName(getId());
		TuanGou tuanGou = this.shopService.getTuangouToday(city);
		return getOrdersByTuanGou(tuanGou);
	}
	
	public String pastOrders(){
		TuanGou tuanGou = this.shopService.findById(getIntegerId());
		return getOrdersByTuanGou(tuanGou);
	}
	
	/**
	 * 查看往期团购的订单
	 * @param tuanGou
	 * @return
	 */
	private String getOrdersByTuanGou(TuanGou tuanGou){
		if(tuanGou != null)
			this.orders = this.orderService.getOrdersByTuangou(tuanGou, getPageIndex());
		return "orders";
	}
	/**
	 * 该城市所有的往期团购
	 * @return
	 */
	public String pastTuans(){
		PageIndex pageIndex = getPageIndex();
		Category city = this.categoryService.findById(getIntegerId());
		this.tuans = this.shopService.listOldTuangou(pageIndex, city);
		return "pastTuans";
	}
	
	/**
	 * 管理员根据不同的售出方式处理订单
	 * @return
	 */
	public String processMes(){
		Order order = this.orderService.findById(getIntegerId());
		if(order.getTuangou().getAccordType() == AccordType.MESSAGE){
			
		}else{
			
		}
		return "process";
	}
	
	public String processExs(){
		Order order = this.orderService.findById(getIntegerId());
		if(order.getTuangou().getAccordType() == AccordType.EXPRESS){
			
		}else{
			
		}
		return "process";
	}
	
	@Override
	public HttpHeaders create() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HttpHeaders editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
